part of '../repository.dart';

class _OneTimePurchasedItemDto extends Equatable {
  const _OneTimePurchasedItemDto._internal({
    required this.orderId,
    required this.purchaseTimeMs,
    required this.acknowledgementStateCode,
    required this.purchaseTypeCode,
  });

  final String orderId;
  final int purchaseTimeMs;
  final int acknowledgementStateCode;
  final int? purchaseTypeCode;

  @override
  List<Object?> get props {
    return [
      orderId,
      purchaseTimeMs,
      acknowledgementStateCode,
      purchaseTypeCode,
    ];
  }

  // factory _OneTimePurchasedItemDto.fromJson(Map<String, dynamic> json) {
  //   return _OneTimePurchasedItemDto._internal(
  //     orderId: json['orderId'],
  //     purchaseTimeMs: int.parse(json['purchaseTimeMillis']),
  //     acknowledgementStateCode: json['acknowledgementState'],
  //     purchaseTypeCode: json['purchaseType'],
  //   );
  // }

  factory _OneTimePurchasedItemDto.fromApiProductPurchase(ProductPurchase apiProductPurchase) {
    return _OneTimePurchasedItemDto._internal(
      orderId: apiProductPurchase.orderId!,
      purchaseTimeMs: int.parse(apiProductPurchase.purchaseTimeMillis!),
      acknowledgementStateCode: apiProductPurchase.acknowledgementState!,
      purchaseTypeCode: apiProductPurchase.purchaseType,
    );
  }

  GooglePlayApiOneTimePurchasedItem toEntity() {
    final purchaseTime = DateTime.fromMillisecondsSinceEpoch(
      purchaseTimeMs,
      isUtc: true,
    );

    return GooglePlayApiOneTimePurchasedItem(
      test: purchaseTypeCode == 0,
      orderId: orderId,
      transactionTime: purchaseTime,
      promotional: purchaseTypeCode == 1,
      acknowledged: acknowledgementStateCode == 1,
    );
  }
}
